using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Services.TfsAlert.Forms;
using Microsoft.Services.TfsAlert.Core.Model;
using Microsoft.Services.TfsAlert.Core.BusinessFacades;
using Microsoft.Services.TfsAlert.Core.BusinessFacades.TeamFoundationBF;
using System.Windows.Forms;

namespace Microsoft.Services.TfsAlert.UIControl
{
    /// <summary>
    /// MVC Controller for the Application Settings dialog box
    /// </summary>
	internal sealed class ApplicationSettingsController
	{
        private ITeamFoundationFacade _foundationFacade = null;
        private IList<TfsServer> _tfsServers = null;
        private ApplicationSettings _appSettingsForm;
        
        #region Event Handlers

        public ApplicationSettingsController()
        {
            IBusinessFacadeFactory factory = BusinessFacadeFactory.CreateInstance();

            _foundationFacade = factory.CreateTeamFoundationBF();

            _tfsServers = _foundationFacade.RetrieveAllServers();
        }

        internal void FormClosing()
        {
            _tfsServers = null;
            _foundationFacade = null;
            _appSettingsForm = null;
        }

        internal void FormLoad()
        {
        }

        #endregion

        #region Support Methods

        internal void Show()
        {
            _appSettingsForm = new ApplicationSettings();

            _appSettingsForm.Show(this);
        }

        internal void SetDataSource(ref IList<TfsServer> tfsList)
        {
            tfsList = _tfsServers;
        }

        internal void FormClose()
        {
            _appSettingsForm.Close();
        }

        private void ErrorMessageDisplay(string message)
        {
            MessageBox.Show(_appSettingsForm, message, "Team Foundation Server Application Settings");
        }

        #endregion

        #region Add-Edit-Delete Methods

        internal bool AddServer()
        {
            bool bRet = false;

            AddEditTfsServer addEditForm = new AddEditTfsServer();

            TfsServer newServer = addEditForm.Show(_appSettingsForm, (TfsServer)null);

            if (newServer != null)
            {
                bRet = true;

                _tfsServers.Add(newServer);
            }

            return bRet;
        }

        internal bool DeleteServer(TfsServer server)
        {
            bool bRet = false;

            DialogResult result = MessageBox.Show(
                string.Format("Are you sure that you want to delete the {0} server information from the list?", server.Name),
                "Delete Server Information",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Question,
                MessageBoxDefaultButton.Button2);

            if (result == DialogResult.Yes)
            {
                try
                {
                    bRet = true;
                    _tfsServers.Remove(server);
                }
                catch (Exception ex)
                {
                    ErrorMessageDisplay(ex.Message);
                }
            }

            return bRet;
        }


        internal void AddEvent(TfsServer tfsServer)
        {
            tfsServer.Events.Add(new TfsEvent());
        }

        internal bool DeleteEvent(TfsServer tfsServer, TfsEvent tfsEvent)
        {
            bool bRet = false;

            DialogResult result = MessageBox.Show(string.Format("Are you sure that you want to delete the {0} event?", tfsEvent.EventType),
                "Delete Event",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Question,
                MessageBoxDefaultButton.Button2);

            if (result == DialogResult.Yes)
            {
                try
                {
                    tfsServer.Events.Remove(tfsEvent);
                    bRet = true;
                }
                catch (Exception ex)
                {
                    ErrorMessageDisplay(ex.Message);
                }
            }

            return bRet;
        }

        internal void EditServer(TfsServer serverIn)
        {
            AddEditTfsServer addEditForm = new AddEditTfsServer();

            addEditForm.Show(_appSettingsForm, serverIn);
        }

        internal void SaveServers()
        {
            _foundationFacade.Update((List<TfsServer>)_tfsServers);
        }

        #endregion

    }
}
